package Base.Loader 
{
	import Base.Log.LogManager;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.LocalConnection;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.system.System;
	
	//import Common.CMessageBox;
	
	/**
	 * ...
	 * @author fox
	 */
	public class TextureLoader extends Loader
	{
		private var loadContext:LoaderContext = null;
		private var url:URLRequest = null;
		
		public var path:String = null;
		public var isLoading:Boolean = false;
		
		public function TextureLoader() 
		{
			
		}
		
		
		public function Load( path:String ):void
		{
			loadContext = new LoaderContext();
			//loadContext.allowCodeImport = true;
			//loadContext.checkPolicyFile = true;
			//loadContext.applicationDomain = ApplicationDomain.currentDomain;
			//loadContext.securityDomain = SecurityDomain.currentDomain;
			
			contentLoaderInfo.addEventListener( Event.COMPLETE , onComplete );
			contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR , onError );
			contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			
			this.path = path;
			url = new URLRequest( path )
			
			load( url , loadContext );
			
			isLoading = true;
		}
		
		
		public function Release():void
		{
			if ( isLoading )
			{
				try
				{
					close();
				}
				catch ( e:Error )
				{
					
				}
			}
			
			isLoading = false;
			
			contentLoaderInfo.removeEventListener( Event.COMPLETE , onComplete );
			contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			
			loadContext = null;
			path = null;
			url = null;
			
			unload();
		}
		
		
		//////////////////////////////////////////////////////////////////
		
		// on complete
		private function onComplete( event:Event ):void
		{
			isLoading = false;
		}
		
		
		// on Error
		private function onError( event:Event ):void
		{
			LogManager.logOut( event.toString() );
			
			isLoading = false;
		}
		
		
		// on Security Error
		private function onSecurityError( event:SecurityErrorEvent ):void
		{
			LogManager.logOut(event.toString());
			
			isLoading = false;
		}
		
	}
	
}